package a06_栈与队列;

import java.util.LinkedList;
import java.util.Queue;

/**
 * <p>
 * a03_用队列实现栈复习4
 * </p>
 *
 * @author flyduck
 * @since 2025/5/15
 */
public class a03_用队列实现栈复习4 {

}
class MyStack5 {

    private Queue<Integer> queue1;
    private Queue<Integer> queue2;

    public MyStack5() {
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }

    public void push(int x) {
        if(queue1.isEmpty() && queue2.isEmpty()){
            queue1.add(x);
        }else if(!queue1.isEmpty()){
            queue1.add(x);
        }else {
            queue2.add(x);
        }
    }

    public int pop() {
        if(!queue1.isEmpty()){
            while (queue1.size() > 1){
                queue2.add(queue1.poll());
            }
            return queue1.poll();
        }else {
            while (queue2.size() > 1){
                queue1.add(queue2.poll());
            }
            return queue2.poll();
        }
    }

    public int top() {
        if(!queue1.isEmpty()){
            while (queue1.size() > 1){
                queue2.add(queue1.poll());
            }
            Integer peek = queue1.peek();
            queue2.add(queue1.poll());
            return peek;
        }else {
            while (queue2.size() > 1){
                queue1.add(queue2.poll());
            }
            Integer peek = queue2.peek();
            queue1.add(queue2.poll());
            return peek;
        }
    }

    public boolean empty() {
        return queue1.isEmpty() && queue2.isEmpty();
    }
}
